package com.vavr.checkincms.util;

import cn.hutool.core.io.copy.StreamCopier;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class FileUtils {
    /**
     * 默认流大小
     */
    private static final  int DEFAULT_BUFFER_SIZE = 2 << 12;


    public static<T> void exportExcel(HttpServletResponse response, String tempFileUrl,
            List<T> userInfos, String filename) {
        if(userInfos == null || userInfos.isEmpty()) {
            log.error("不存在需要导出的数据!");
            return;
        }

        try (ExcelWriter excelWriter = EasyExcel.write(tempFileUrl, userInfos.getFirst().getClass()).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet(1, "高峰会签到记录").build();
            excelWriter.write(userInfos, writeSheet);
        }
        catch (Exception e) {
            log.error("生成导出excel异常,{}", e.getMessage());
        }
        response.setHeader("Content-disposition",
                "attachment;filename="+ filename +";filename*=utf-8''"+filename);
        ServletOutputStream os = null;
        InputStream is = null;

        try {
            os = response.getOutputStream();
            is = new BufferedInputStream(new FileInputStream(new File(tempFileUrl)));
            new StreamCopier(DEFAULT_BUFFER_SIZE, -1, null).copy(is, os);
        }
        catch (IOException e) {
            log.error("输出excel流异常,{}", e.getMessage());
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            }
            catch (IOException e) {
                log.error("关闭流失败{}", e.getMessage());
            }
        }
    }


    public static<T> void exportExcelByMap(HttpServletResponse response, String tempFileUrl,
            List<T> userInfos, String filename) {
        if(userInfos == null || userInfos.isEmpty()) {
            log.error("不存在需要导出的数据!");
            return;
        }

        try (ExcelWriter excelWriter = EasyExcel.write(tempFileUrl, CellData.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet(1, "高峰会签到记录").build();
            excelWriter.write(userInfos, writeSheet);
        }
        catch (Exception e) {
            log.error("生成导出excel异常,{}", e.getMessage());
        }
        response.setHeader("Content-disposition",
                "attachment;filename="+ filename +";filename*=utf-8''"+filename);
        ServletOutputStream os = null;
        InputStream is = null;

        try {
            os = response.getOutputStream();
            is = new BufferedInputStream(new FileInputStream(new File(tempFileUrl)));
            new StreamCopier(DEFAULT_BUFFER_SIZE, -1, null).copy(is, os);
        }
        catch (IOException e) {
            log.error("输出excel流异常,{}", e.getMessage());
        }
        finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            }
            catch (IOException e) {
                log.error("关闭流失败{}", e.getMessage());
            }
        }
    }
}
